package com.woke.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public class Customer {
	private int id;
	private String cusname;
	private String cuscreatetime;
	private String cusupdatetime;
	private String isprize;
	private String isdele;
	private String belongId;
	
	
	public String getBelongId() {
		return belongId;
	}
	public void setBelongId(String belongId) {
		this.belongId = belongId;
	}
	public String getIsdele() {
		return isdele;
	}
	public void setIsdele(String isdele) {
		this.isdele = isdele;
	}
	public String getIsprize() {
		return isprize;
	}
	public void setIsprize(String isprize) {
		this.isprize = isprize;
	}
	public String getCusname() {
		return cusname;
	}
	public void setCusname(String cusname) {
		this.cusname = cusname;
	}
	public String getCuscreatetime() {
		return cuscreatetime;
	}
	public void setCuscreatetime(String cuscreatetime) {
		this.cuscreatetime = cuscreatetime;
	}
	public String getCusupdatetime() {
		return cusupdatetime;
	}
	public void setCusupdatetime(String cusupdatetime) {
		this.cusupdatetime = cusupdatetime;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	
	public Customer() {

	}
	
	public Customer(int id,String belongId, String cusname,String cuscreatetime,String cusupdatetime,String isprize,String isdele) {
		super();
		this.id = id;
		this.belongId=belongId;
		this.cusname = cusname;
		this.cuscreatetime=cuscreatetime;
		this.cusupdatetime=cusupdatetime;
		this.isprize=isprize;
		this.isdele=isdele;
	}

	public static List<Customer> getData(String sql) {
		Connection con = null;
		Statement st = null;
		ResultSet rs = null;
		List<Customer> list = new ArrayList<Customer>();
		try {
			con = WokeConnection.getConnection();
			st = con.createStatement();
			rs = st.executeQuery(sql);
			while (rs.next()) {
				int id = rs.getInt("id");
				String belongId = rs.getString("belongId");
				String cusname = rs.getString("cusname");
				String cuscreatetime = rs.getString("cuscreatetime");
				String cusupdatetime = rs.getString("cusupdatetime");
				String isprize = rs.getString("isprize");
				String isdele = rs.getString("isdele");
				Customer u = new Customer(id,belongId, cusname,  cuscreatetime, cusupdatetime,isprize,isdele);
				list.add(u);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			WokeConnection.closeMySql(con, st, rs);
		}
		return list;
	}
	
	public int selectLendSize(){
		Connection conn = WokeConnection.getConnection();
		PreparedStatement pst=null;
		ResultSet rs=null;
		try {
			 pst = conn.prepareStatement("select count(*) from customer");
			 rs = pst.executeQuery();
			if(rs.next()){
				int pagecount = rs.getInt(1);
				return pagecount;
			}
			return 0;
		} catch (Exception e) {

			// TODO Auto-generated catch block

			e.printStackTrace();

			return 0;

		}finally{
			WokeConnection.closeMySql(conn, pst, rs);
		}
	}
	
	public List<Customer> selectLend(int pageNow, int pageSize){

		List<Customer> list = new ArrayList<Customer>();
		Connection conn = WokeConnection.getConnection();
		PreparedStatement pst=null;
		ResultSet rs=null;
		
		try {
			int startIndex =0;
			int endIndex = pageSize;
			String sql = "select  * from customer ";
			String sw = " where " + " 1=1 " +"and isdele=1"+" order by id Desc ";
			// sql +=" where " + strWhere ;
			if (pageNow > 1) {
				startIndex = pageSize * (pageNow - 1);
				// endIndex = pageSize * currentPage;
			}
			sql += sw + " limit " + startIndex + "," + endIndex;
			
			 pst= conn.prepareStatement(sql);
			 rs= pst.executeQuery();
			while(rs.next()){
				Customer s= new Customer();
				s.setId(rs.getInt(1));
				s.setBelongId(rs.getString(2));
				s.setCusname(rs.getString(3));
				s.setCuscreatetime(rs.getString(4));
				s.setCusupdatetime(rs.getString(5));
				s.setIsprize(rs.getString(6));
				s.setIsdele(rs.getString(7));
				list.add(s);
			}
			return list;
		} catch (Exception e) {

			// TODO Auto-generated catch block

			e.printStackTrace();

			return null;

		}finally{

			WokeConnection.closeMySql(conn, pst, rs);

		}

	}
	
	
	
	public List<Customer> selectLend1(int pageNow, int pageSize){

		List<Customer> list = new ArrayList<Customer>();
		Connection conn = WokeConnection.getConnection();
		PreparedStatement pst=null;
		ResultSet rs=null;
		
		try {
			int startIndex =0;
			int endIndex = pageSize;
			String sql = "select  * from customer ";
			String sw = " where " + " 1=1 " +"and isdele=0"+" order by id Desc ";
			// sql +=" where " + strWhere ;
			if (pageNow > 1) {
				startIndex = pageSize * (pageNow - 1);
				// endIndex = pageSize * currentPage;
			}
			sql += sw + " limit " + startIndex + "," + endIndex;
			
			 pst= conn.prepareStatement(sql);
			 rs= pst.executeQuery();
			while(rs.next()){
				Customer s= new Customer();
				s.setId(rs.getInt(1));
				s.setBelongId(rs.getString(2));
				s.setCusname(rs.getString(3));
				s.setCuscreatetime(rs.getString(4));
				s.setCusupdatetime(rs.getString(5));
				s.setIsprize(rs.getString(6));
				s.setIsdele(rs.getString(7));
				list.add(s);
			}
			return list;
		} catch (Exception e) {

			// TODO Auto-generated catch block

			e.printStackTrace();

			return null;

		}finally{

			WokeConnection.closeMySql(conn, pst, rs);

		}

	}
	
	public List<Customer> selectLend2(int pageNow, int pageSize){

		List<Customer> list = new ArrayList<Customer>();
		Connection conn = WokeConnection.getConnection();
		PreparedStatement pst=null;
		ResultSet rs=null;
		
		try {
			int startIndex =0;
			int endIndex = pageSize;
			String sql = "select  * from customer ";
			String sw = " where " + " 1=1 " +"and isprize='yes'"+" order by id Desc ";
			// sql +=" where " + strWhere ;
			if (pageNow > 1) {
				startIndex = pageSize * (pageNow - 1);
				// endIndex = pageSize * currentPage;
			}
			sql += sw + " limit " + startIndex + "," + endIndex;
			
			 pst= conn.prepareStatement(sql);
			 rs= pst.executeQuery();
			while(rs.next()){
				Customer s= new Customer();
				s.setId(rs.getInt(1));
				s.setBelongId(rs.getString(2));
				s.setCusname(rs.getString(3));
				s.setCuscreatetime(rs.getString(4));
				s.setCusupdatetime(rs.getString(5));
				s.setIsprize(rs.getString(6));
				s.setIsdele(rs.getString(7));
				list.add(s);
			}
			return list;
		} catch (Exception e) {

			// TODO Auto-generated catch block

			e.printStackTrace();

			return null;

		}finally{

			WokeConnection.closeMySql(conn, pst, rs);

		}

	}
	
	public List<Customer> selectLend3(int pageNow, int pageSize){

		List<Customer> list = new ArrayList<Customer>();
		Connection conn = WokeConnection.getConnection();
		PreparedStatement pst=null;
		ResultSet rs=null;
		
		try {
			int startIndex =0;
			int endIndex = pageSize;
			String sql = "select  * from customer ";
			String sw = " where " + " 1=1 " +"and isprize='no'"+" order by id Desc ";
			// sql +=" where " + strWhere ;
			if (pageNow > 1) {
				startIndex = pageSize * (pageNow - 1);
				// endIndex = pageSize * currentPage;
			}
			sql += sw + " limit " + startIndex + "," + endIndex;
			
			 pst= conn.prepareStatement(sql);
			 rs= pst.executeQuery();
			while(rs.next()){
				Customer s= new Customer();
				s.setId(rs.getInt(1));
				s.setBelongId(rs.getString(2));
				s.setCusname(rs.getString(3));
				s.setCuscreatetime(rs.getString(4));
				s.setCusupdatetime(rs.getString(5));
				s.setIsprize(rs.getString(6));
				s.setIsdele(rs.getString(7));
				list.add(s);
			}
			return list;
		} catch (Exception e) {

			// TODO Auto-generated catch block

			e.printStackTrace();

			return null;

		}finally{

			WokeConnection.closeMySql(conn, pst, rs);

		}
	}
	
	
}
